package com.smbms.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smbms.entity.Provider;
import com.smbms.entity.User;
import com.smbms.service.provider.ProviderService;
import com.smbms.tool.PageSupport;

@Controller
@RequestMapping("provider")
public class ProviderController {
	@Resource
	private ProviderService providerService;
	
	/**
	 * 分页显示供应商列表
	 * @param queryProCode
	 * @param queryProName
	 * @param pageIndex
	 * @param model
	 * @return
	 */
	@RequestMapping("list")
	public String list(String queryProCode,String queryProName,String pageIndex,Model model){
		
		int currentPageNo=1;//当前页码
    	if(pageIndex!=null){
    		currentPageNo=Integer.valueOf(pageIndex);
    	}
		
		Integer pageSize = 5;
		
		Integer fromIndex=(currentPageNo-1)*pageSize;//起始数字
		
		int totalCount = providerService.count(queryProName, queryProCode);
		
		PageSupport pageSupport = new PageSupport();
		pageSupport.setTotalCount(totalCount);
		pageSupport.setPageSize(pageSize);
		pageSupport.setCurrentPageNo(currentPageNo);
		
		int totalPageCount = pageSupport.getTotalPageCount();
		
		model.addAttribute("totalPageCount",totalPageCount);
		model.addAttribute("totalCount",totalCount);
		model.addAttribute("currentPageNo",currentPageNo);
		
		List<Provider> providers = providerService.getProviderList(queryProName, queryProCode, fromIndex, pageSize);
		model.addAttribute("queryProCode",queryProCode);
		model.addAttribute("queryProName",queryProName);
		model.addAttribute("providerList",providers);
		return "providerlist";
	}
	
	/**
	 * 去到添加供应商页面
	 * @return
	 */
	@RequestMapping(value="toAdd")
	public String toAdd(){
		return "provideradd";
	}
	
	/**
	 * 获取所有供应商信息
	 * @return
	 */
	@RequestMapping("getProName")
	@ResponseBody
	public Object getProName(){
		List<Provider> providers = providerService.findProviderAll();
		return providers;
	}
	
	/**
	 * 添加供应商
	 * @param provider
	 * @param session
	 * @return
	 */
	@RequestMapping(value="add")
	public String add(Provider provider,HttpSession session){
		
		User user = (User)session.getAttribute("userSession");
		provider.setCreatedBy(user.getId());
		provider.setCreationDate(new Date());
		boolean flag = providerService.add(provider);
		if(flag){
			return "redirect:list";
		}else{
			return "provideradd";
		}
		
	}
	
	/**
	 * 显示供应商视图
	 * @param proid
	 * @param model
	 * @return
	 */
	@RequestMapping("view")
	public String view(String proid,Model model){
		int id = Integer.parseInt(proid);
		Provider provider = providerService.getProviderById(id);
		model.addAttribute("provider",provider);
		return "providerview";
	}
	
	/**
	 * 去到修改供应商页面
	 * @param proid
	 * @param model
	 * @return
	 */
	@RequestMapping("toModify")
	public String toModify(String proid,Model model){
		int id = Integer.parseInt(proid);
		Provider provider = providerService.getProviderById(id);
		model.addAttribute(provider);
		return "providermodify";
	}
	
	/**
	 * 修改供应商
	 * @param provider
	 * @param session
	 * @return
	 */
	@RequestMapping("modify")
	public String modify(Provider provider,HttpSession session){
		User user = (User)session.getAttribute("userSession");
		provider.setModifyBy(user.getId());
		provider.setModifyDate(new Date());
		boolean flag = providerService.modify(provider);
		if(flag){
			return "redirect:list";
		}else{
			return "providermodify";
		}
	}
	
	/**
	 * 删除供应商
	 * @param proid
	 * @return
	 */
	@RequestMapping("delete")
	@ResponseBody
	public Object delete(String proid){
		int id = Integer.parseInt(proid);
		boolean flag = providerService.deleteProviderById(id);
		return flag;
	}
	
	
	
}
